package com.deepblue.image.maker.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.deepblue.image.maker.common.CommonResult;
import com.deepblue.image.maker.config.DockerConfigProperties;
import com.deepblue.image.maker.config.FreemakerProperties;
import com.deepblue.image.maker.domain.freemaker.Dockerfile;
import com.deepblue.image.maker.domain.vo.req.CreateDockerfileVO;
import com.deepblue.image.maker.handler.WebsocketPrintHandler;
import com.deepblue.image.maker.service.DockerfileService;
import com.deepblue.image.maker.util.CommonResultUtil;
import com.deepblue.image.maker.websocket.WebsocketSessionCache;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LoggingBuildHandler;
import com.spotify.docker.client.exceptions.DockerException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * 镜像接口
 *
 * @author yymagicer
 * @version 1.0
 * @date 2021/11/9 上午10:50
 */
@RestController
@RequestMapping("/image")
@Api(value = "镜像接口管理",tags = "镜像接口管理")
@Slf4j
public class ImageApi {


    @Resource
    private DockerfileService dockerfileService;


    @Resource
    private FreemakerProperties freemakerProperties;

    @Resource
    private DockerConfigProperties dockerConfigProperties;

    @Resource
    private DockerClient dockerClient;

    @Resource
    private WebsocketSessionCache websocketSessionCache;

    @PostMapping("/create")
    @ApiOperation("创建镜像")
    public CommonResult<Void> create(@Validated @RequestBody CreateDockerfileVO vo){
        Dockerfile dockerfile = BeanUtil.toBean(vo, Dockerfile.class);
        String dockerFilePath = dockerfileService.create(dockerfile);
        String projectName =  StringUtils.isBlank(vo.getProjectName()) ? dockerConfigProperties.getHarbor().getDefaultProjectName() : vo.getProjectName();
        String imageFullPath = dockerConfigProperties.getHarbor().getAddress()+StrUtil.SLASH +projectName+ StrUtil.SLASH+vo.getImageName()+StrUtil.COLON+vo.getTag();
        try {
            WebsocketPrintHandler websocketPrintHandler = new WebsocketPrintHandler(websocketSessionCache,"yymagicer");
            dockerClient.build(Paths.get(freemakerProperties.getWriteLocation()), imageFullPath,dockerFilePath, websocketPrintHandler);
            dockerClient.push(imageFullPath,websocketPrintHandler);
        }catch (DockerException | InterruptedException | IOException e) {
            e.printStackTrace();
            log.error("使用dockerfile构建镜像失败",e);
        }
        return CommonResultUtil.success();
    }
}
